package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import data.User;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import service.AuthorService;
import service.UserService;


@SuppressWarnings("serial")
public class AuthorServlet extends HttpServlet
{
	/**
	 * get与post的参数名
	 * 一般在getParameter方法中使用
	 * */
	public enum ArgName
	{
		GET_GET("get"),
		
		POST_TYPE("type"),
		POST_PEN_NAME("pen_name"),
		;
		
		String name;
		
		private ArgName(String name)
		{
			this.name = name;
		}
		
		public String getName()
		{
			return this.name;
		}
	}
	
	
	/**
	 * get的参数值
	 * 一般会用到getValue()
	 * */
	public enum GetValue
	{
		IS_AUTHOR,//get
		PEN_NAME,//get
		;
		
		public String getValue()
		{
			return this.toString();
		}
	}
	
	
	/**
	 * post的参数值
	 * 一般会用到getValue()
	 * */
	public enum PostValue
	{
		REGISTER,//type
		;
		
		public String getValue()
		{
			return this.toString();
		}
	}
	
	private static UserService service_user = new UserService();
	private static AuthorService service_author = new AuthorService();
	
	public AuthorServlet()
	{
		
	}
	
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException
	{
		System.out.println(this.toString() + ".doGet");
		String arg_get = request.getParameter(AuthorServlet.ArgName.GET_GET.getName());
		
		if (arg_get == null || arg_get.equals(""))
			return;
		
		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession();
		String username = (String)session.getAttribute(User.Attribute.USERNAME.getFullName());
		String password = (String)session.getAttribute(User.Attribute.PASSWORD.getFullName());
		
		if (username == null || password == null)
			return;
		else
			if (AuthorServlet.service_user.login(username, password) != UserService.ReturnStatus.SUCCESS)
				return;
		
		if (arg_get.equals(AuthorServlet.GetValue.IS_AUTHOR.getValue()))
		{
			if (AuthorServlet.service_author.isAuthor(username))
				out.print(true);
			else
				out.print(false);
		}
		else if(arg_get.equals(AuthorServlet.GetValue.PEN_NAME.getValue()))
		{
			String pen_name = AuthorServlet.service_author.getPenName(username);
			if (pen_name != null)
				out.print(pen_name);
		}
		
		out.flush();
		out.close();
	}
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		System.out.println(this.toString() + ".doPost");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		response.setContentType("text/plain;charset=UTF-8");
		
		String arg_type = request.getParameter(AuthorServlet.ArgName.POST_TYPE.getName());
		
		if (arg_type.equals(AuthorServlet.PostValue.REGISTER.getValue()))
		{
			String pen_name = request.getParameter(AuthorServlet.ArgName.POST_PEN_NAME.getName());
			if (pen_name == null || pen_name.equals(""))
			{
				//out.println("请输入笔名");
				out.println("input your pen name please");
				out.flush();
				out.close();
				return;
			}
			
			String username = (String)session.getAttribute(User.Attribute.USERNAME.getFullName());
			
			if (username != null)
			{
				if (!AuthorServlet.service_author.isAuthor(username))
				{
					int id = AuthorServlet.service_author.getID(username);
					AuthorServlet.service_author.register(id, pen_name);
					//out.println("注册成功");
					out.println("register success");
				}
				else
				{
					//out.println("该账号已注册过作者");
					out.println("you have been a author");
				}
			}
		}
		else
		{
			//out.println("传参错误");
			out.println("error parameter");
		}
		
		out.flush();
		out.close();
	}
}









